package com.dp.appi.controller;

import com.dp.appi.entity.UserEntity;
import com.dp.appi.exception.CommonException;
import com.dp.appi.exception.ExplorationRuntimeException;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.ExplorationService;
import com.dp.appi.service.PTService;
import com.dp.appi.service.PackageService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/5/9.
 * 踏勘点
 */
@Controller
@RequestMapping(value = "/explorationControl")
public class ExplorationControl {
    private static Logger log = LoggerFactory.getLogger(ExplorationControl.class);
    @Autowired
    private ExplorationService explorationService;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private PTService ptService;
    @Autowired
    private PackageService packageService;

    /**
     * 获取踏勘点列表
     *
     * @param modelMap
     * @param param
     * @param stage_id         项目阶段id
     */
    @RequestMapping(value = "/explorationList", method = RequestMethod.GET)
    public void explorationList(ModelMap modelMap, @RequestParam Map<String, String> param,  Long stage_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            List<Map<String, Object>> list = explorationService.explorationList(param.get("exploration_type"), stage_id, String.valueOf(map.get("id")),param.get("explor_id"));
            modelMap.addAttribute("explorationList", list);
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        } catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取踏勘点(定位点+10kv)列表
     * @param modelMap
     * @param param

     */
    @RequestMapping(value = "/explorationList_high", method = RequestMethod.GET)
    public void explorationList_high(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
           List<Map<String, Object>> list = explorationService.explorationList_high(param.get("exploration_type"), Long.valueOf(param.get("stage_id")), String.valueOf(map.get("id")));
            modelMap.addAttribute("explorationList", list);
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        } catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 删除踏勘点
     *
     * @param modelMap
     * @param param          参数
     * @param exploration_id 踏勘点id
     */
    @RequestMapping(value = "/delExplor", method = RequestMethod.POST)
    public void delExplor(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            //逻辑删除点位状态
            explorationService.delExplor(exploration_id, String.valueOf(map.get("id")));

            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        } catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取踏勘点信息【APP端点位查看】
     *
     * @param modelMap
     * @param param
     * @param exploration_id 踏勘点id
     */
    @RequestMapping(value = "/explorInfo", method = RequestMethod.GET)
    public void explorInfo(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id) {
        try {
            modelMap.addAllAttributes(explorationService.explorInfo(exploration_id));
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 踏勘点信息设置
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/addExploration", method = RequestMethod.POST)
    public void addExploration(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Long exploration_id = explorationService.addExploration(param, String.valueOf(map.get("id")));
            modelMap.addAttribute("exploration_id", exploration_id);
            modelMap.addAttribute("id", param.get("pt_objectid"));
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
    /**
     * 踏勘点信息设置
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/addBj", method = RequestMethod.POST)
    public void addBj(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            Long exploration_id = explorationService.addBj(param, String.valueOf(map.get("id")));
            modelMap.addAttribute("msg", "保存成功");
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        }catch (Exception e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 踏勘点信息继续设置
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/conAddExploration", method = RequestMethod.POST)
    public void conAddExploration(ModelMap modelMap, @RequestParam Map<String, Object> param) {
        try {
            Object exploration_id = param.get("exploration_id");
            if(null == exploration_id||"".equals(String.valueOf(exploration_id))){
                throw new CommonException("EXPO012");
            }
            Long explorationId = Long.parseLong(String.valueOf(exploration_id));
            Map<String, Object> expMap = explorationService.explorInfo(explorationId);
            if (null == expMap) {
                throw new CommonException("EXPO010");
            }
            //更新典设
            Object codeset_id = param.get("codeset_id");
            if(null != codeset_id&&!"".equals(String.valueOf(codeset_id))){
                explorationService.upCodeSet(Long.valueOf(String.valueOf(codeset_id)),explorationId);
            }

            //更新或新增扩展
            if (null == expMap.get("pt_objectid")) {
                ptService.newPt(param, Integer.parseInt(String.valueOf(expMap.get("pt_id"))),explorationId);
            } else {
                ptService.upPt(param, Integer.parseInt(String.valueOf(expMap.get("pt_id"))), Long.parseLong(String.valueOf(expMap.get("pt_objectid"))));
            }
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 修改踏勘点信息【app端点击点位修改信息】
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/upExploration", method = RequestMethod.POST)
    public void upExploration(ModelMap modelMap, @RequestParam Map<String, Object> param) {
        try {
            //当前点位
            Object exploration_id = param.get("exploration_id");
            //当前点位的档距
            Object distance = param.get("distance");

            if (null == exploration_id || "".equals(String.valueOf(exploration_id))) {
                throw new CommonException("EXPO012");
            }
            Long explorationId = Long.parseLong(String.valueOf(exploration_id));
            Map<String, Object> expMap = explorationService.explorInfo(explorationId);//获取点位信息
            if (null == expMap) {
                throw new CommonException("EXPO010");
            }

            //原来的上联点位
            Object old_before_exp_id = expMap.get("before_exp");
            param.put("old_before_exp_id", old_before_exp_id);

            //现在的上联点位
            Object new_before_exp_id = param.get("before_exp");

            /**
             * 修改踏勘信息
             number、 before_exp、exploration_type、pt_id、lng、lat、corner、alt、distance、location_way
             */
            //这一步就已经将需要修改的当前点位的信息修改了
            explorationService.upExploration(param, explorationId);//app端点击点位修改信息


            if( null != new_before_exp_id){
                //修改杆塔信息上联，同时修改交叉跨院的上联(不允许修改同批量设计其他点位的档距信息)
                explorationService.upjckyExploration(explorationId,Long.parseLong(String.valueOf(new_before_exp_id) ));
            }

            if (null != param.get("pt")) {
                String ptObj = String.valueOf(param.get("pt"));
                Map<String, Object> ptParam = new Gson().fromJson(ptObj, new TypeToken<Map<String, String>>() {
                }.getType());

                    //新增定位点扩展
                if (null == expMap.get("pt_objectid")) {
                    ptService.newPt(ptParam, Integer.parseInt(String.valueOf(expMap.get("pt_id"))), explorationId);
                } else {
                    //更新定位点扩展【批量更新了同期点位的span值，a_pole表】
                    if(new_before_exp_id != exploration_id){
                        ptService.upPt(ptParam, Integer.parseInt(String.valueOf(expMap.get("pt_id"))), Long.parseLong(String.valueOf(expMap.get("pt_objectid"))));
                    }
                }
            }

            //原来的上联点位和现在的上联点位进行判断
            if (null != old_before_exp_id && null != new_before_exp_id){
                if (!String.valueOf(new_before_exp_id).equals(String.valueOf(old_before_exp_id))) {
                    //更新角度
                    packageService.calculateConer(Long.valueOf(String.valueOf(old_before_exp_id)));
                    packageService.calculateConer(explorationId);

                    //更新台区---更换
                    explorationService.updateExp_id(explorationId, Long.valueOf(String.valueOf(new_before_exp_id)));
                }
            }else if (null == old_before_exp_id && null != new_before_exp_id) {
                //更新角度
                packageService.calculateConer(explorationId);
                //更新台区---更换
                explorationService.updateExp_id(explorationId, Long.valueOf(String.valueOf(new_before_exp_id)));
            }else if(null != old_before_exp_id && null == new_before_exp_id){
                //更新台区---更换
                explorationService.updateExp_id(explorationId, Long.valueOf(String.valueOf(old_before_exp_id)));
            }

            //更新相关点位角度和档距

            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取定位点上级定位点
     *
     * @param modelMap
     * @param param
     * @param exploration_id 当前踏勘id
     */
    @RequestMapping(value = "/beforeExp", method = RequestMethod.GET)
    public void beforeExp(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id) {
        try {
            modelMap.addAttribute("afterExp", explorationService.beforeExp(exploration_id));
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取台区
     *
     * @param modelMap
     * @param param
     * @param stage_id 阶段id
     */
    @RequestMapping(value = "/transformerList", method = RequestMethod.GET)
    public void transformerList(ModelMap modelMap, @RequestParam Map<String, String> param, Long stage_id) {
        try {
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            modelMap.addAttribute("explorationList", explorationService.transformerList(stage_id, String.valueOf(map.get("id"))));
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 计算新踏勘点选择上联后，与上联形成夹角。备注：计算出的夹角存储在上联的corner上
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/expCorner", method = RequestMethod.GET)
    public void expCorner(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            //lng  新踏勘点经度,lat 新踏勘点纬度,before_id  选择的上联id
            Long exploration_id;
            Object explorationId = param.get("exploration_id");
            if(null==explorationId||explorationId.equals(""))
                exploration_id = Long.valueOf(-1);
            else
                exploration_id = Long.valueOf(String.valueOf(explorationId));

            Double lng = Double.valueOf(param.get("lng"));
            Double lat = Double.valueOf(param.get("lat"));
            Long before_id = Long.valueOf(param.get("before_id"));
            String type = param.get("type");

            modelMap.addAttribute("corner", explorationService.returnCorner(exploration_id,lng, lat, before_id,type));
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 排直功能后点位坐标返回
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/exp_obj_up", method = RequestMethod.GET)
    public void exp_obj_up(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            //lng  新踏勘点经度,lat 新踏勘点纬度,before_id  选择的上联id
            Double lng = Double.valueOf(param.get("lng"));
            Double lat = Double.valueOf(param.get("lat"));
            Long before_id = Long.valueOf(param.get("before_id"));

            modelMap.addAttribute("exp_obj_up", explorationService.exp_obj(lng,lat,before_id));
        } catch (ExplorationRuntimeException e) {
            modelMap.addAllAttributes(SYS.valueOf(e.getMessage()).msgMap());
        }catch (CommonException c){
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
}
